package tostr;

/*
+------------+
|2019/12/10  |
|14:50       |
+------------+
  */
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class ToStr {

    public static void createAsciiPic(final String path) {
        //设置的字符由复杂到简单排列
        final String base = "9876543210";
//        final String base = "#@&sri%*!~;,`.";
//        final String base = "■";
        //final String base = "sri%*!~;,`.";
//         String base = "KSPksp";
//        final String base = "###";
        try {
            final BufferedImage image = ImageIO.read(new File(path));
            System.out.println("W:"+image.getWidth()+" H:"+image.getHeight());
            for (int y = 0; y < image.getHeight(); y +=2) {
                for (int x = 0; x < image.getWidth(); x++) {
                    final int pixel = image.getRGB(x, y);
                    final int r = (pixel & 0xff0000) >> 16,
                            g = (pixel & 0xff00) >> 8,
                            b = pixel & 0xff;
                    final float gray = 0.299f * r + 0.578f * g + 0.114f * b;
                    final int index = Math.round(gray * (base.length() + 1) / 255);
                    System.out.print(index >= base.length() ? " " : String.valueOf(base.charAt(index)));
                }
                System.out.println();
            }
        } catch (final IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String args []){
        //设置图片文件的绝对路径
        ToStr.createAsciiPic("D:\\jpgtostr\\kurumi.jpg");
    }
}
/*
效果图
                      ,*&#######################r;                     
                .!i@###@&r%~;,.           .,;!i&@###@i;                
             ,%@##@i*,                            ;%s###i,             
           !@##s!.                                    !s##s;           
         !&#&!                       ...                ,r##r`         
       `s#@%.                     ,i@&&#s,                ~&#@!        
      !##s,    ,~;`             .i##%   %#~                .r##*       
     %##%    !@#s!!@&%         .i###@~   *s.                 %##*      
    *##%   .i###&,   .#~       ~@###&;    @,                 .r##~     
   !##%    !@####%     #,     .%###r,.   .@,                  ;@#r.    
  .##s    `r####&~             ;&#####*  .`                    i##~    
  *#@~     !@######`             %###&;                        ~@#i    
  @#s.       ~r&@@&`                                           ;&#r    
 .##i                                                          ;@#i    
  ##r                 !@&r!*s&i.                               !@#%    
  @#s.                  ..           .                         %##~    
  *#@~           .`      .````.       ..                      ;&#r.    
  .&#s.         ``   `%s*!~;~~!!%&r,   .                      %##&;    
   ,@#r        .,  ;##;  ,,,,;~*&&;    .                     ~@####;   
    ;@#r.       ;,   *#########@*     .`                   .%##%`r#@`  
     ;##&,        `.   *##%~i#i.     ..                   !&##%  ~@#%  
      .s##r`                                            *@####r` `&#@  
        `s###i;                                  .!~ .i##&r&#@~   r##  
           ,i@###s%,                             r##########&,   `s##  
               `~s@##*                             `~i&&s%,.     %##!  
                  *@#~                                         ,s##%   
                 ,&#@                                        ;s##s`    
                 %##~                                      *@##r`      
                `s##                                    `r##@*         
                *@#%                                   `r#s;           
                i##,  ~##                              !#@~            
               ,s#&.  s#&                             .r#s,            
               ;&#i  ,##%                             ;&#%             
               ~@#!  *##.                             !#@*             
               !@#;  *##                              !#&;             
*/